package com.ciquan.server.controller;

import com.ciquan.server.bean.KeyValue;
import com.ciquan.server.service.KeyValueService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by bjliwei2014 on 2015/6/5.
 */
@Controller
@RequestMapping("/keyWords")
public class KeyWordsController extends AbstractController {

    @Resource
    private KeyValueService keyValueService;

    @RequestMapping("/list")
    public ModelAndView list(ModelMap modelMap) {
        KeyValue keyValue = new KeyValue();
        keyValue.setTitle("keywords");
        keyValue = keyValueService.selectOne(keyValue);
        modelMap.put("keyValue", keyValue);
        return new ModelAndView("keyWords", modelMap);
    }

    @RequestMapping("/save")
    public ModelAndView list(HttpServletRequest request) {
        int id = ServletRequestUtils.getIntParameter(request, "id", 0);
        String content = ServletRequestUtils.getStringParameter(request, "content", "");
        KeyValue keyValue = new KeyValue();
        keyValue.setId(id);
        keyValue.setTitle("keywords");
        keyValue.setContent(content);
        keyValueService.update(keyValue);
        return new ModelAndView("redirect:/keyWords/list");
    }
}
